package fdq.controller;

import fdq.mapper.MessageMapper;
import fdq.mapper.UserMapper;
import fdq.pojo.Message;
import fdq.service.MessageService;
import fdq.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class showMessages {
    MessageService messageService = new MessageService();
    @RequestMapping("/messageBoard")
    public ModelAndView show(){
        Message[] allMessages = messageService.selectAllMessages();
        ModelAndView mv = new ModelAndView();
        mv.addObject("allMessages",allMessages);
        mv.setViewName("messageBoard");
        return mv;
    }

    @RequestMapping("/insert")
    public ModelAndView insert(@RequestParam(value = "mname")String mname,
                               @RequestParam(value = "message") String message){
        messageService.insertMessage(mname,message);
        Message[] allMessages = messageService.selectAllMessages();
        ModelAndView mv = new ModelAndView();
        mv.addObject("allMessages",allMessages);
        mv.setViewName("messageBoard");
        return mv;
    }

    @RequestMapping("/delete")
    public ModelAndView delete(@RequestParam(value = "mid") int mid){
        System.out.println("delete被访问");
        messageService.deleteMessage(mid);
        Message[] allMessages = messageService.selectAllMessages();
        ModelAndView mv = new ModelAndView();
        mv.addObject("allMessages",allMessages);
        mv.setViewName("messageBoard");
        return mv;
    }
}
